﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherGivingBirthFile
{
    /// <summary>
    /// 分娩修改
    /// </summary>
    public class MotherGivingBirthUpdateHanler : IRequestHandler<MotherGivingBirthUpdateCommon, Result>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<MotherGivingBirth> motherGivinBirthRep;
        private readonly IBaseRepository<SleepInfor> sleepinforRep;
        /// <summary>
        /// 分娩修改
        /// </summary>
        /// <param name="motherGivinBirthRep"></param>
        /// <param name="_sleepinforRep"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public MotherGivingBirthUpdateHanler(IBaseRepository<MotherGivingBirth> motherGivinBirthRep, IBaseRepository<SleepInfor> _sleepinforRep, IMapper _mapper)
        {
            this.motherGivinBirthRep = motherGivinBirthRep ?? throw new ArgumentNullException(nameof(motherGivinBirthRep));
            this.sleepinforRep = _sleepinforRep ?? throw new ArgumentNullException(nameof(_sleepinforRep));
            mapper = _mapper ?? throw new ArgumentNullException(nameof(mapper));
        }
        /// <summary>
        /// 分娩修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(MotherGivingBirthUpdateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result { Code = (int)CodeEnum.update };
            try
            {
                var mothergivinbirth = mapper.Map<MotherGivingBirth>(request);
                var n = motherGivinBirthRep.Update(mothergivinbirth);
                if (n > 0)
                {
                    result.Code = (int)CodeEnum.success;
                    result.Messege = "修改成功";

                }
            }
            catch (Exception ms)
            {
                result.Code = (int)CodeEnum.error;
                throw;
            }
            return result;
        }
    }
}
